<?php
namespace App\Http\Controllers;

use App\DBs\UserDB;
use Laravel\Lumen\Routing\Controller as BaseController;

class Controller extends BaseController
{
    //最基本控制器

    protected $db;

    public function __construct()
    {
    }

    /**
     * 返回APP端的Json数据
     * @param $code 错误代码
     * @param $data 返回的数据
     * @param $msg 自定义提示
     * return json
     */
    public function toAppJson($code=0, $data='',$total=0)
    {
        $msgArr = array(
            0 => '操作成功', -500 => '操作失败',
            -1 => '接口权限错误', -2 => '参数不全或错误',  -3 => '没有数据',
            -4 => '登陆错误',  -5 => '',  -6 => '',
        );
        $msgStr = array_key_exists($code,$msgArr) ? $msgArr[$code] : '请求失败';
        $jsonArr = array('code' => $code, 'msg' => $msgStr);
        if ($data) {
            $jsonArr['total'] = $total; $jsonArr['data'] = $data;
        }
        return $jsonArr;
    }

    /**
     * 返回WX端的Json数据
     * @param $code 错误代码
     * @param $data 返回的数据
     * @param $msg 自定义提示
     * return json
     */
    public function toWxJson($code=0, $data='',$total=0)
    {
        $msgArr = array(
            0 => '操作成功', -500 => '操作失败',
            -1 => '接口权限错误', -2 => '参数不全或错误',  -3 => '没有数据',
            -4 => '登陆错误',  -5 => '',  -6 => '',
        );
        $msgStr = array_key_exists($code,$msgArr) ? $msgArr[$code] : '请求失败';
        $jsonArr = array('code' => $code, 'msg' => $msgStr);
        if ($data) {
            $jsonArr['total'] = $total; $jsonArr['data'] = $data;
        }
        return $jsonArr;
    }

    /**
     * 获取header参数
     */
    public function getHeader($key)
    {
        $header = '';
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $ucwords = strtolower(str_replace('_', ' ', substr($name, 5)));
                //$headers[str_replace(' ', '-', $ucwords)] = $value;
                if (str_replace(' ', '-', $ucwords)==$key) {
                    $header = $value;
                }
            }
        }
        return $header;
    }

    /**
     * 文件大小分级
     */
    public function getSizeLevel($size)
    {
        if ($size>1024 && $size<=1024*1024) {
            return round($size/1024,2) . 'KB';
        }
        else if ($size>1024*1024 && $size<=1024*1024*1024) {
            return round($size/1024/1024,2) . 'MB';
        }
        else if ($size>1024*1024*1024) {
            return round($size/1024/1024/1024,2) . 'GB';
        }
        else {
            return $size . 'B';
        }
    }

    /**
     * 构建CURL请求
     * @param $string 参数拼接的字符串
     * @return $json
     */
    public function setCurl($url='', $string='')
    {
        if (!$url) {
            return array('code' => -2, 'msg' => 'URL地址不能空');
        }
        $curl = curl_init(); //初始化
        curl_setopt($curl,CURLOPT_URL,$url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $string);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json', 'Content-Length: ' . strlen($string)
        ));
        curl_setopt($curl, CURLOPT_TIMEOUT, 5);
        $output = curl_exec($curl);
        curl_close($curl);
        if (!$output) {
            return array('code' => -1, 'msg' => '请求失败');
        }
        return json_decode($output,true);
    }
}